Make SourceKind private
authorAlex Crichton <alex@alexcrichton.com>
Thu, 23 Oct 2014 19:01:48 +0000 (12:01 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 27 Oct 2014 19:40:23 +0000 (12:40 -0700)
This is a bit of an implementation detail of the `SourceId` and we should be
able to get away without exposing it.

src/bin/git_checkout.rs
src/cargo/core/mod.rs
src/cargo/core/package_id.rs
src/cargo/core/source.rs
src/cargo/ops/cargo_rustc/fingerprint.rs
src/cargo/ops/registry.rs
src/cargo/sources/git/source.rs
src/cargo/util/toml.rs
tests/resolve.rs

index a903f28d7229710fdcebbce4342ebf6b3809434e..a42cbbd03fa41b82173f695f85465b46a01169a6 100644 (file)
@@ -30,7 +30,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>
                    })
                    .map_err(|e| CliError::from_boxed(e, 1)));
 
-    let source_id = SourceId::for_git(&url, reference.as_slice(), None);
+    let source_id = SourceId::for_git(&url, reference.as_slice());
 
     let mut config = try!(Config::new(shell, None, None).map_err(|e| {
         CliError::from_boxed(e, 1)
index 50d86caa4d7cc4747b6fabf68d18bee6f6d5b635..f7ffee33701207d3224c4d8a5a2ea35ba79b4cb0 100644 (file)
@@ -6,8 +6,7 @@ pub use self::package_id_spec::PackageIdSpec;
 pub use self::registry::Registry;
 pub use self::resolver::Resolve;
 pub use self::shell::{Shell, MultiShell, ShellConfig};
-pub use self::source::{PathKind, RegistryKind};
-pub use self::source::{Source, SourceId, SourceMap, SourceSet, GitKind};
+pub use self::source::{Source, SourceId, SourceMap, SourceSet};
 pub use self::summary::Summary;
 
 pub mod source;
index 1442a781563a3927443162574680894e2d8d6306..f9ee93b4030d9b8ae6544aa891147a83ce7a70f3 100644 (file)
@@ -159,13 +159,13 @@ impl Show for PackageId {
 #[cfg(test)]
 mod tests {
     use super::{PackageId, CENTRAL_REPO};
-    use core::source::{RegistryKind, SourceId};
+    use core::source::SourceId;
     use util::ToUrl;
 
     #[test]
     fn invalid_version_handled_nicely() {
         let loc = CENTRAL_REPO.to_url().unwrap();
-        let repo = SourceId::new(RegistryKind, loc);
+        let repo = SourceId::for_registry(&loc);
 
         assert!(PackageId::new("foo", "1.0", &repo).is_err());
         assert!(PackageId::new("foo", "1", &repo).is_err());
index 743c48f7f47406bb5e41c39a9ac459ced4260ef6..08a105b0e601d6c9e49bbea4308a7212dbe1c70d 100644 (file)
@@ -46,7 +46,7 @@ pub trait Source: Registry {
 }
 
 #[deriving(Encodable, Decodable, Show, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
-pub enum SourceKind {
+enum SourceKind {
     /// GitKind(<git reference>) represents a git repository
     GitKind(String),
     /// represents a local path
@@ -72,7 +72,7 @@ struct SourceIdInner {
 }
 
 impl SourceId {
-    pub fn new(kind: SourceKind, url: Url) -> SourceId {
+    fn new(kind: SourceKind, url: Url) -> SourceId {
         SourceId {
             inner: Arc::new(SourceIdInner {
                 kind: kind,
@@ -109,7 +109,8 @@ impl SourceId {
                 }
                 url.query = None;
                 let precise = mem::replace(&mut url.fragment, None);
-                SourceId::for_git(&url, reference.as_slice(), precise)
+                SourceId::for_git(&url, reference.as_slice())
+                         .with_precise(precise)
             },
             "registry" => {
                 let url = url.to_url().unwrap();
@@ -155,9 +156,8 @@ impl SourceId {
         Ok(SourceId::new(PathKind, url))
     }
 
-    pub fn for_git(url: &Url, reference: &str, precise: Option<String>) -> SourceId {
+    pub fn for_git(url: &Url, reference: &str) -> SourceId {
         SourceId::new(GitKind(reference.to_string()), url.clone())
-                 .with_precise(precise)
     }
 
     pub fn for_registry(url: &Url) -> SourceId {
@@ -173,7 +173,6 @@ impl SourceId {
     }
 
     pub fn get_url(&self) -> &Url { &self.inner.url }
-    pub fn get_kind(&self) -> &SourceKind { &self.inner.kind }
     pub fn is_path(&self) -> bool { self.inner.kind == PathKind }
     pub fn is_registry(&self) -> bool { self.inner.kind == RegistryKind }
 
@@ -196,7 +195,9 @@ impl SourceId {
                 };
                 box PathSource::new(&path, self) as Box<Source>
             },
-            RegistryKind => box RegistrySource::new(self, config) as Box<Source+'a>,
+            RegistryKind => {
+                box RegistrySource::new(self, config) as Box<Source+'a>
+            }
         }
     }
 
@@ -204,6 +205,13 @@ impl SourceId {
         self.inner.precise.as_ref().map(|s| s.as_slice())
     }
 
+    pub fn git_reference(&self) -> Option<&str> {
+        match self.inner.kind {
+            GitKind(ref s) => Some(s.as_slice()),
+            _ => None,
+        }
+    }
+
     pub fn with_precise(&self, v: Option<String>) -> SourceId {
         SourceId {
             inner: Arc::new(SourceIdInner {
index 4027d3a2fbfe83c29fb936eaee27b21b7affffb0..195e75caeb6a2213930c2501baec4dcfe40bb11b 100644 (file)
@@ -3,7 +3,7 @@ use std::hash::{Hash, Hasher};
 use std::hash::sip::SipHasher;
 use std::io::{fs, File, USER_RWX, BufferedReader};
 
-use core::{Package, Target, PathKind};
+use core::{Package, Target};
 use util;
 use util::{CargoResult, Fresh, Dirty, Freshness, internal, Require, profile};
 
@@ -54,10 +54,7 @@ pub fn prepare_target(cx: &mut Context, pkg: &Package, target: &Target,
     // constant (which is the responsibility of the source)
     let use_pkg = {
         let doc = target.get_profile().is_doc();
-        let path = match *pkg.get_summary().get_source_id().get_kind() {
-            PathKind => true,
-            _ => false,
-        };
+        let path = pkg.get_summary().get_source_id().is_path();
         doc || !path
     };
 
index 2998dda1e9cb126477b1a3791ba50cf21d3f735b..fbef2c6291726b81cbaf0a5d034d514001206dbc 100644 (file)
@@ -7,7 +7,7 @@ use git2;
 use registry::{Registry, NewCrate, NewCrateDependency};
 
 use core::source::Source;
-use core::{Package, MultiShell, SourceId, RegistryKind};
+use core::{Package, MultiShell, SourceId};
 use core::manifest::ManifestMetadata;
 use ops;
 use sources::{PathSource, RegistrySource};
index c2731fa786d62ef5466f1d3ebd232b29abfb6c16..870bcaa3e800056b5b7f62b708d07514d7544dbf 100644 (file)
@@ -4,7 +4,7 @@ use std::hash::sip::SipHasher;
 use std::mem;
 use url::{mod, Url};
 
-use core::source::{Source, SourceId, GitKind};
+use core::source::{Source, SourceId};
 use core::{Package, PackageId, Summary, Registry, Dependency};
 use util::{CargoResult, Config, to_hex};
 use sources::PathSource;
@@ -28,9 +28,9 @@ impl<'a, 'b> GitSource<'a, 'b> {
                        config: &'a mut Config<'b>) -> GitSource<'a, 'b> {
         assert!(source_id.is_git(), "id is not git, id={}", source_id);
 
-        let reference = match *source_id.get_kind() {
-            GitKind(ref reference) => reference,
-            _ => fail!("Not a git source; id={}", source_id)
+        let reference = match source_id.git_reference() {
+            Some(reference) => reference,
+            None => fail!("Not a git source; id={}", source_id),
         };
 
         let remote = GitRemote::new(source_id.get_url());
index 465280331de56a987eb1f8b432e0cc0a430c79b8..27082011d64bdcf808294dc9c92763c29ba12c1d 100644 (file)
@@ -9,7 +9,7 @@ use toml;
 use semver;
 use serialize::{Decodable, Decoder};
 
-use core::{SourceId, GitKind};
+use core::SourceId;
 use core::manifest::{LibKind, Lib, Dylib, Profile, ManifestMetadata};
 use core::{Summary, Manifest, Target, Dependency, PackageId};
 use core::package_id::Metadata;
@@ -526,11 +526,10 @@ fn process_dependencies<'a>(cx: &mut Context<'a>, dev: bool,
 
         let new_source_id = match details.git {
             Some(ref git) => {
-                let kind = GitKind(reference.clone());
                 let loc = try!(git.as_slice().to_url().map_err(|e| {
                     human(e)
                 }));
-                Some(SourceId::new(kind, loc))
+                Some(SourceId::for_git(&loc, reference.as_slice()))
             }
             None => {
                 details.path.as_ref().map(|path| {
index 30c8c04fca857e5e17cf5d6d863f3de6571d6d04..44fae3425be5ec734b75a283acc5c121508e65fe 100644 (file)
@@ -7,7 +7,7 @@ use std::collections::HashMap;
 
 use hamcrest::{assert_that, equal_to, contains};
 
-use cargo::core::source::{SourceId, RegistryKind, GitKind};
+use cargo::core::source::SourceId;
 use cargo::core::{Dependency, PackageId, Summary, Registry};
 use cargo::util::{CargoResult, ToUrl};
 use cargo::core::resolver::{mod, ResolveEverything};
@@ -29,7 +29,7 @@ trait ToDep {
 impl ToDep for &'static str {
     fn to_dep(self) -> Dependency {
         let url = "http://example.com".to_url().unwrap();
-        let source_id = SourceId::new(RegistryKind, url);
+        let source_id = SourceId::for_registry(&url);
         Dependency::parse(self, Some("1.0.0"), &source_id).unwrap()
     }
 }
@@ -71,7 +71,7 @@ macro_rules! pkg(
 
 fn registry_loc() -> SourceId {
     let remote = "http://example.com".to_url().unwrap();
-    SourceId::new(RegistryKind, remote)
+    SourceId::for_registry(&remote)
 }
 
 fn pkg(name: &str) -> Summary {
@@ -84,8 +84,7 @@ fn pkg_id(name: &str) -> PackageId {
 
 fn pkg_id_loc(name: &str, loc: &str) -> PackageId {
     let remote = loc.to_url();
-    let source_id = SourceId::new(GitKind("master".to_string()),
-                                  remote.unwrap());
+    let source_id = SourceId::for_git(&remote.unwrap(), "master");
 
     PackageId::new(name, "1.0.0", &source_id).unwrap()
 }
@@ -97,13 +96,13 @@ fn pkg_loc(name: &str, loc: &str) -> Summary {
 fn dep(name: &str) -> Dependency { dep_req(name, "1.0.0") }
 fn dep_req(name: &str, req: &str) -> Dependency {
     let url = "http://example.com".to_url().unwrap();
-    let source_id = SourceId::new(RegistryKind, url);
+    let source_id = SourceId::for_registry(&url);
     Dependency::parse(name, Some(req), &source_id).unwrap()
 }
 
 fn dep_loc(name: &str, location: &str) -> Dependency {
     let url = location.to_url().unwrap();
-    let source_id = SourceId::new(GitKind("master".to_string()), url);
+    let source_id = SourceId::for_git(&url, "master");
     Dependency::parse(name, Some("1.0.0"), &source_id).unwrap()
 }